package com.zjitc.serviceedu.controller;
import com.alipay.api.internal.util.AlipaySignature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduCourse;
import com.zjitc.serviceedu.entity.EduOrder;
import com.zjitc.serviceedu.entity.vo.OrderVo;
import com.zjitc.serviceedu.service.EduOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-22
 */
@RestController
@RequestMapping("/serviceedu/edu-order")
@Api(tags = "订单管理")
public class EduOrderController {


    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
    @Autowired
    private EduOrderService orderService;
    @ApiOperation("查询所有订单")
    @GetMapping("OrderList")
    public RespBean OrderList(EduOrder eduOrder) {
        List<EduOrder> list = orderService.list(null);
        return RespBean.success().data("list",list).msg("查询成功");
    }

    @ApiOperation("添加订单")
    @PostMapping("addOrder")
    public RespBean addOrder(EduOrder eduOrder) {
        boolean result = orderService.saveOrder(eduOrder);
        if(!result) {
            throw new AuthException("添加失败");
        }

        return RespBean.success().msg("添加成功");
    }


    @ApiOperation("修改详情查询")
    @GetMapping("getById/{id}")
    public RespBean getById(@PathVariable("id") Long id) {
        EduOrder order = orderService.getById(id);
        if(order == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", order).msg("查询成功");
    }
    @ApiOperation("根据课程查询订单")
    @GetMapping("getCourseOrder/{courseId}")
    public RespBean getCourseOrder(@PathVariable("courseId") Long courseId) {
        // 使用Lambda查询，根据课程ID查找订单
        QueryWrapper<EduOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        EduOrder order = orderService.getOne(queryWrapper);

        // 如果没有找到订单，抛出自定义异常
        if (order == null) {
            throw new AuthException("查询失败");
        }

        // 返回查询结果
        return RespBean.success().data("data", order).msg("查询成功");
    }

    @ApiOperation("修改订单")
    @PutMapping("updateOrder")
    public RespBean updateOrder(EduOrder eduOrder) {
        boolean result = orderService.updateById(eduOrder);
        if(!result) {
            throw new AuthException("修改失败");
        }

        return RespBean.success().msg("修改成功");
    }

    @ApiOperation("删除订单")
    @DeleteMapping("delOrder/{id}")
    public RespBean delOrder(@PathVariable("id") Long id) {
        boolean result = orderService.removeById(id);
        if(!result) {
            throw new AuthException("删除失败");
        }

        return RespBean.success().msg("删除成功");
    }

    @ApiOperation("批量删除订单")
    @DeleteMapping("delMoreOrder")
    public RespBean delMoreRole(Long[] ids) {
        boolean result = orderService.removeByIds(Arrays.asList(ids));
        if(!result) {
            throw new AuthException("批量删除失败");
        }

        return RespBean.success().msg("批量删除成功");
    }
    @ApiOperation("订单条件查询")
    @GetMapping("getPageBatchs")
    public RespBean getPageBatchs(OrderVo orderVo) {

        Page<EduOrder> page = new Page<>(orderVo.getPage(), orderVo.getSize());

        QueryWrapper<EduOrder> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(orderVo.getNickname())) {
            queryWrapper.like("nickname", orderVo.getNickname());
        }
        if(!StringUtils.isEmpty(orderVo.getPhone())) {
            queryWrapper.like("phone", orderVo.getPhone());
        }
        if (!StringUtils.isEmpty(orderVo.getCourseTitle())) {
            queryWrapper.like("course_title", orderVo.getCourseTitle());
        }

        queryWrapper.orderByAsc("id");
        orderService.page(page, queryWrapper);

        if(page == null) {
            throw new AuthException("查询失败");
        }

        List<EduOrder> list = page.getRecords();
        Long total = page.getTotal();

        if (list.isEmpty()) {
            // 可以选择返回空列表或抛出异常，这里选择返回空列表和提示信息
            return RespBean.success().data("list", list).data("total", total).msg("未找到符合条件的订单");
        }

        return RespBean.success().data("list", list).data("total", total).msg("查询成功");
    }

    @ApiOperation("根据用户查询订单")
    @GetMapping("getUserOrder{userId}")
    public RespBean getUserOrder(@PathVariable("userId") Long userId){
        QueryWrapper<EduOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<EduOrder> order =  orderService.list(queryWrapper);

        // 如果没有找到订单，抛出自定义异常
        if (order == null) {
            throw new AuthException("查询失败");
        }

        // 返回查询结果
        return RespBean.success().data("data", order).msg("查询成功");
    }

}

